package com.supplychain.scm.core.dinger.config;

import com.supplychain.scm.core.dinger.aspect.DingdingTalkAspect;
import com.supplychain.scm.core.dinger.util.DingDingUtils;
import com.github.jaemon.dinger.core.entity.DingerProperties;
import com.github.jaemon.dinger.support.CustomMessage;
import com.github.jaemon.dinger.support.DingerAsyncCallback;
import com.github.jaemon.dinger.support.DingerExceptionCallback;
import com.github.jaemon.dinger.support.DingerIdGenerator;
import com.github.jaemon.dinger.support.client.DingerHttpClient;
import com.github.jaemon.dinger.support.sign.DingerSignAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.concurrent.Executor;

import static com.github.jaemon.dinger.constant.DingerConstant.*;

/**
 * @author liyang
 * @version 1.0.0
 * @ClassName DingDingTalkAutoConfiguration
 * @createTime 20:00:00 2022/11/18
 */
@ConditionalOnProperty(prefix = DingDingProperties.PREFIX, name = "annotation", havingValue = "true")
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties({DingDingProperties.class})
public class DingDingTalkAutoConfiguration {
	//private final DingerRootSend dingerSender;
	@Resource
	private ApplicationContext applicationContext;
	@Resource
	private DingerExceptionCallback dingerExceptionCallback;
	@Resource
	private DingerSignAlgorithm dingerSignAlgorithm;
	@Resource
	@Qualifier(TEXT_MESSAGE)
	private CustomMessage textMessage;
	@Resource
	@Qualifier(MARKDOWN_MESSAGE)
	private CustomMessage markDownMessage;
	@Resource
	private DingerIdGenerator dingerIdGenerator;
	@Autowired(required = false)
	@Qualifier(DINGER_EXECUTOR)
	private Executor dingTalkExecutor;
	@Resource
	private DingerAsyncCallback dingerAsyncCallback;
	@Resource
	private DingerHttpClient dingerHttpClient;
	@Resource
	private DingerProperties dingerProperties;

	@Bean
	@ConditionalOnMissingBean
	public DingerRootSend dingerRootSend() {
		DingerRootSend dingerRootSend = new DingerRootSend();
		dingerRootSend.setDingerExceptionCallback(dingerExceptionCallback);
		dingerRootSend.setDingerSignAlgorithm(dingerSignAlgorithm);
		dingerRootSend.setTextMessage(textMessage);
		dingerRootSend.setMarkDownMessage(markDownMessage);
		dingerRootSend.setDingerIdGenerator(dingerIdGenerator);
		dingerRootSend.setDingTalkExecutor(dingTalkExecutor);
		dingerRootSend.setDingerAsyncCallback(dingerAsyncCallback);
		dingerRootSend.setDingerHttpClient(dingerHttpClient);
		dingerRootSend.setDingerProperties(dingerProperties);
		dingerRootSend.setApplicationContext(applicationContext);
		return dingerRootSend;
	}

	@Bean
	@ConditionalOnMissingBean
	public DingDingUtils dingDingUtils(DingerRootSend dingerRootSend){
		DingDingUtils dingDingUtils = new DingDingUtils();
		dingDingUtils.setDingerSender(dingerRootSend);
		return dingDingUtils;
	}

	@Bean
	@ConditionalOnMissingBean
	public DingdingTalkAspect dingdingTalkAspect() {

		return new DingdingTalkAspect(applicationContext);
	}
}
